package com.link.base.base.contractsinfo.service;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.link.base.base.annualplan.dao.mybatis.mapper.AnnualPlanMapper;
import com.link.base.base.annualplan.model.AnnualPlan;
import com.link.base.base.approval.model.Flow;
import com.link.base.base.approval.service.FlowService;
import com.link.base.base.authorizationbrand.model.AuthorizationBrand;
import com.link.base.base.authorizationbrand.service.AuthorizationBrandService;
import com.link.base.base.authproduct.model.AuthProduct;
import com.link.base.base.authproduct.service.AuthProductService;
import com.link.base.base.contractsinfo.dao.mybatis.mapper.ContractsInfoMapper;
import com.link.base.base.contractsinfo.model.ContractApprovalInfo;
import com.link.base.base.contractsinfo.model.ContractsInfo;
import com.link.base.base.product.model.PriceList;
import com.link.base.base.product.model.PriceListItem;
import com.link.base.base.product.service.PriceListItemService;
import com.link.base.base.product.service.PriceListService;
import com.link.core.basic.dao.mapper.BasicMapper;
import com.link.core.basic.service.ServiceException;
import com.link.core.basic.service.BasicServiceImpl;
import com.link.core.util.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author liushoulong
 * @date 2018-11-05
 * @desc 合同信息Service
 */
@Service
public class ContractsInfoServiceImpl extends BasicServiceImpl<ContractsInfo> implements ContractsInfoService {

    /**
     * 修改合同状态为" 签订中"
     **/
    private static final String CONSTRCT_STATUS_SIGNING = "Signing";
    /**
     * 年度规划: 审批中
     **/
    private static final String AUDIT_STATUS_APPROVING = "Approving";

    @Resource
    private ContractsInfoMapper contractsInfoMapper;
    @Resource
    private FlowService flowService;
    @Resource
    private AnnualPlanMapper annualPlanMapper;
    @Resource
    private AuthorizationBrandService authorizationBrandService;
    @Resource
    private PriceListService priceListService;
    @Resource
    private PriceListItemService priceListItemService;
    @Resource
    private AuthProductService authProductService;

    @Override
    public BasicMapper<ContractsInfo> getBasicMapper() {
        return contractsInfoMapper;
    }


    /**
     * 合同签约提交审批
     *
     * @param entity
     * @param approvalDetail
     * @throws Exception
     */
    @Override
    @Transactional(rollbackFor = ServiceException.class)
    public void contractSignSubmit(ContractsInfo entity, String approvalDetail) throws Exception {
        String approvalType = "ContractSign";
        //创建审批流程
        flowService.createFlow(entity.getId(), approvalType, 0L, entity.getAccntName(), entity.getAccntId(), approvalDetail);

        /*修改合同状态为“签订中”*/
        entity.setContractStatus(CONSTRCT_STATUS_SIGNING);
        contractsInfoMapper.update(entity);

        /*修改年度规划审核状态为“签订中”*/
        AnnualPlan annualPlan = new AnnualPlan();
        annualPlan.setContractId(entity.getId());
        annualPlan.setPageFlag(false);
        List<AnnualPlan> annualPlanList = annualPlanMapper.queryByExamplePage(annualPlan);
        for (AnnualPlan a : annualPlanList) {
            a.setAuditStatus(AUDIT_STATUS_APPROVING);
            annualPlanMapper.update(a);
        }
    }

    /**
     * 合同签约审批处理-审批成功后更新客户信息和合同信息
     *
     * @param entity
     * @throws Exception
     */
    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public void contractSignObjectUpdate(ContractApprovalInfo entity) throws Exception {
        ObjectMapper mapper = new ObjectMapper();
        Flow flow = new Flow();
        flow.setId(entity.getFlowId());
        Flow flowObj = flowService.queryById(flow);

        String flag = "Y";
        if (flag.equals(flowObj.getCurrentIsWrite())) {
            /*审批详情封装json字符串*/
            String approvalDetail = mapper.writeValueAsString(entity);
            /*审批流程更新*/
            flowObj.setFlowObjDetail(approvalDetail);
            flowService.update(flowObj);
        }
        if (StringUtils.isBlank(flowObj.getNewStepNum())) {
            /*合同信息更新*/
            ContractsInfo contract = entity.getContractObj();
            getBasicMapper().update(contract);

            /*授权品牌信息*/
            List<AuthorizationBrand> authBrandList = entity.getAuthBrandList();
            if (authBrandList.size() > 0) {
                authorizationBrandService.batchUpdate(authBrandList);
            }

            Long priceListId = contract.getPriceListId();
            PriceList priceList = new PriceList();
            priceList.setId(priceListId);
            PriceList priceListObj = priceListService.queryById(priceList);
            /**
             * @TODO 待确定
             */

            /*授权单品信息*/
            List<AuthProduct> autoProdList = entity.getAuthProdList();
            for (AuthProduct authProduct : autoProdList) {
                PriceListItem priceListItem = new PriceListItem();
                priceListItem.setHeadId(priceListId);
                priceListItem.setProdId(authProduct.getProdId());
                List<PriceListItem> priceListItemList = priceListItemService.queryByProdId(priceListItem);
                if (priceListItemList.size() > 0) {
                    authProduct.setSalesPrice(priceListItemList.get(0).getPromoPrice());
                    authProduct.setPriceUnit(priceListItemList.get(0).getPriceUnit());
                }
                PriceListItem priceListItem1 = new PriceListItem();
                /**
                 * @TODO 待确定
                 */
                priceListItem.setProdId(authProduct.getProdId());
                List<PriceListItem> purPriceListItemList = priceListItemService.queryByProdId(priceListItem);
                if (purPriceListItemList.size() > 0) {
                    /**
                     * @TODO 待确定
                     */

                }
                authProduct.setProductStatus("Active");
                authProductService.insert(authProduct);
            }
        }
    }


    /**
     * 合同打印申请审批处理-状态修改
     *
     * @param objectId
     * @param param1
     * @param param2
     * @param param3
     * @param param4
     * @param param5
     * @throws Exception
     */
    @Override
    @Transactional(rollbackFor = ServiceException.class)
    public void contractPrintStatusUpdate(Long objectId, String param1, String param2,
                                          String param3, String param4, String param5) throws Exception {
        String rejected = "Rejected";
        String approved = "Approved";
        /*合同信息*/
        ContractsInfo queryObject = new ContractsInfo();
        queryObject.setId(objectId);
        ContractsInfo entity = queryById(queryObject);
        if (entity == null) {
            throw new ServiceException("CONTRACTSINFO-003");
        }
        //审批拒绝
        if (rejected.equals(param1)) {
            /*修改合同【状态】为“签约失败”*/
            entity.setContractStatus(param2);
            contractsInfoMapper.update(entity);
        }
        //审批通过
        if (approved.equals(param1)) {
            /*修改合同【状态】为“已生效”*/
            entity.setContractStatus(param2);
            contractsInfoMapper.update(entity);
        }
    }
}
